package
{
	import as3isolib.geom.Pt;
	
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.filters.GlowFilter;
	import flash.geom.Point;

	public class InputManager
	{
		private var m_gameModeStack:Vector.<IGameMode> = new Vector.<IGameMode>;
		
		private var m_mouseDown:Boolean = false;
		private var m_oldMousePoint:Point;
		private var m_mouseWasDragging:Boolean = false;		
		
		public function InputManager(stage:Stage)
		{
			m_gameModeStack.push(new GMDefault());
			stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
			stage.addEventListener(Event.RESIZE, onStageResize);
		}
		
		public function addGameMode(gameMode:IGameMode):void{
			m_gameModeStack.unshift(gameMode);
		}
		
		public function popGameMode():void{
			m_gameModeStack.shift();
		}
		
		private function onStageResize():void{
			
		}
		
		private function onKeyUp(ke:KeyboardEvent):void{
			
		}
		
		private function onMouseDown(me:MouseEvent):void{
			m_mouseDown = true;
			var mouseDownPoint:Point = new Point(me.stageX, me.stageY);
			m_oldMousePoint = mouseDownPoint;
			
			m_gameModeStack[0].onMouseDown(me);
		}
		
		private function onMouseUp(me:MouseEvent):void{
			m_mouseDown = false;
			
			if(!m_mouseWasDragging){
				m_gameModeStack[0].onMouseUp(me);	
			}			
		}
		
		private function onMouseMove(me:MouseEvent):void{
			var mouseIsDragging:Boolean = m_mouseDown;
			var currentPosition:Point = new Point(me.stageX, me.stageY);
			var isoPosition:Pt = Global.isoView.localToIso(currentPosition);
			
			if(mouseIsDragging){
				m_mouseWasDragging = true;
				var deltaPosition:Point = m_oldMousePoint.subtract(currentPosition);
				Global.isoView.panBy(deltaPosition.x, deltaPosition.y);
			} else {
				m_mouseWasDragging = false;
				m_gameModeStack[0].onMouseMove(me);
			}
			
			m_oldMousePoint = currentPosition.clone();			
		}
	}
}